package juc.threadpool;

import java.util.concurrent.*;


/**
 * 提交优先级
 * 执行优先级
 *
 * IO密集型/CPU密集型
 */
public class ThreadPoolDemo {

    public static void main(String[] args) {
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();//快
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);//慢
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();//最慢

        RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                //写数据库的代码
            }
        };

        //自定义线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                10,
                20,
                0L,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(10),rejectedExecutionHandler);//自定义线程池


        for (int i=1;i<=100;i++){
            threadPoolExecutor.execute(new MyTask(i));
        }
    }

}

class MyTask implements Runnable{

    int i=0;

    public MyTask(int i){
        this.i=i;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"程序员做第"+i+"个项目");
        try {
            Thread.sleep(3000);//业务逻辑
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
